package com.ytkj.digitalConstruction.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * 使用Spring的RestTemplate进行REST接口请求
 * 
 * @author Pete
 * @Date 2017年12月8日
 */
public class RestUtil {

	private static RestTemplate restTemplate = new RestTemplate();

	/**
	 * 发送GET请求
	 * 
	 * @param url
	 * @param returnClassName
	 * @param parameters
	 * @return
	 */
	public static <T> T get(String url, Class<T> returnClassName, Map<String, Object> parameters) {
		if (parameters == null) {
			return restTemplate.getForObject(url, returnClassName);
		}
		return restTemplate.getForObject(url, returnClassName, parameters);
	}

	/**
	 * 发送POST请求
	 * 
	 * @param url
	 * @param returnClassName
	 * @param inputHeader
	 * @param inputParameter
	 * @param jsonBody
	 * @return
	 */
	public static <T> T post(String url, Class<T> returnClassName, Map<String, Object> inputHeader,
			Map<String, Object> inputParameter, String jsonBody) {
		// 请求Header
		HttpHeaders httpHeaders = new HttpHeaders();
		// 拼接Header
		if (inputHeader != null) {
			Set<String> keys = inputHeader.keySet();
			for (Iterator<String> i = keys.iterator(); i.hasNext();) {
				String key = (String) i.next();
				httpHeaders.add(key, inputHeader.get(key).toString());
			}
		}
		// 设置请求的类型及编码
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		httpHeaders.setContentType(type);
		httpHeaders.add("Accept", "application/json");
		List<MediaType> acceptableMediaTypes = new ArrayList<>();
		acceptableMediaTypes.add(MediaType.ALL);
		httpHeaders.setAccept(acceptableMediaTypes);
		HttpEntity<String> formEntity = new HttpEntity<String>(jsonBody, httpHeaders);
		if (inputParameter == null) {
			return restTemplate.postForObject(url, formEntity, returnClassName);
		}
		return restTemplate.postForObject(url, formEntity, returnClassName, inputParameter);
	}
}